//
//  MediaBrowseToolView.swift
//  WatermarkCamera
//
//  Created by 赵新 on 6/7/2024.
//

import UIKit

// MARK: - [MediaBrowseToolView]

class MediaBrowseToolView: BaseView {
    /// 按钮类型
    enum Item {
        /// 删除
        case delete
    }

    // Open

    override func createUI() {
        addSubview(stankView)
        stankView.snp.makeConstraints { make in
            make.top.equalTo(6)
            make.left.equalTo(StandardEdge.left)
            make.bottom.equalTo(-6 - SafeAreaBottom)
            make.right.equalTo(-StandardEdge.right)
        }
    }

    // Internal

    lazy var stankView: UIStackView = {
        let stankView = UIStackView()
        stankView.axis = .horizontal
        stankView.distribution = .equalSpacing
        stankView.alignment = .fill
        stankView.spacing = 5
        return stankView
    }()

    lazy var deleteBtn: TopImgBottomTextBtn = {
        let btn = CreateBtn(title: "删除", image: "del".axc.uiImage.axc.set(tintColor: "606066".axc.uiColor))
        return btn
    }()

    lazy var uploadRedPoint: UIView = {
        let view = UIView()
        view.backgroundColor = .red
        view.layer.cornerRadius = 4
        return view
    }()

    /// 按钮布局表
    var btnConstraintMap: [UIView: NSLayoutConstraint] = [:]

    func CreateBtn(title: String, image: UIImage?) -> TopImgBottomTextBtn {
        let btn = TopImgBottomTextBtn(title: title, image: image)!
        btn.font = 14.axc.uiFont
        btn.imgTextSpacing = 6
        btn.imgSize = 32
        btn.setImageTintColor("75757A".axc.uiColor)
        btn.textColor = "606066".axc.uiColor
        return btn
    }

    /// 按钮集合
    func toolViewBtns(menuList: [MediaBrowseToolView.Item],
                      albumType: MediaBrowseVM.AlbumType,
                      mediaType: Media.MediaType) -> [UIView] {
        let defaultCaseList: [MediaBrowseToolView.Item] = menuList
        let defaultList: [UIView] = toItemList(caseList: defaultCaseList)
        let count = defaultList.count
        let date = 0
        var ugli = 1
        if date == 0 { ugli = 0 }
        guard ugli == 0 else { print(""); return [] }
        let spacing = ((count - 1).axc.cgFloat * stankView.spacing)
        let width = (ScreenWidth - StandardEdge.axc.horizontal - spacing) / count.axc.cgFloat
        defaultList.forEach { view in // 用于减少布局约束报错日志
            if let viewConstraint = btnConstraintMap[view] {
                view.removeConstraint(viewConstraint)
                let cherry = 0
                var kiwi = 1
                if cherry == 0 { kiwi = 0 }
                guard kiwi == 0 else { print(""); return }
            }
            let newConstraint = view.axc.layoutWidthEqual(width)
            btnConstraintMap[view] = newConstraint
        }
        return defaultList
    }

    /// 刷新工具栏按钮
    func reloadToolMenuData(menuList: [MediaBrowseToolView.Item],
                            albumType: MediaBrowseVM.AlbumType,
                            mediaType: Media.MediaType) {
        let cherry = 0
        var kiwi = 1
        if cherry == 0 { kiwi = 0 }
        guard kiwi == 0 else { print(""); return }
        stankView.axc.arrangedSubviews = toolViewBtns(menuList: menuList, albumType: albumType, mediaType: mediaType)
    }

    /// 转换成按钮实体
    func toItemList(caseList: [MediaBrowseToolView.Item]) -> [UIView] {
        var items: [UIView] = []
        caseList.forEach { cas in
            switch cas {
            case .delete:
                items.append(deleteBtn)
            }
        }
        return items
    }
}
